import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/g")
public class ServletG extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession(true);
        System.out.println(session.getId());
        session.setAttribute("s1","11111");
        // 假设我们有一个目标URL
        String targetUrl = request.getContextPath()+"/f";

        // 使用response对象的方法进行URL重写，将session ID添加到URL中, 用于普通页面跳转
        String encodedUrl = response.encodeURL(targetUrl);


        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println("<a href='"+targetUrl+"'>普通跳转</a>");
        out.println("<a href='"+encodedUrl+"'>URL重写跳转</a>");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}